<?php

defined('BASEPATH') or die('forbidden');

class Petnew_Controller Extends Controller {

    private $rolename;
    private $dbConnt;	//数据库连接
    private $fightlist;
    private $statuslist;	//宠物状态
    private $nameList;
    private $m;	//数据库连接
    private $server_info;
	
    function _init() {
        check_level(50, segment(0), segment(1));
    	set_time_limit(0);
		$this->inc = $this->config['inc'];
        //$this->titlelist = get_type_title();
        $this->fightlist=array('0'=>'未出战','1'=>'已出战队伍1','2'=>'已出战队伍2');
        $this->statuslist=array('0'=>'未删除','1'=>'已删除');
        //得到选择的区号
        $areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }
        //query server mapping
        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='1' AND areanum={$areanum} ";
        //$selectSql = "SELECT * FROM server_mapping WHERE dbtype='1'";
        $this->db->query($selectSql);
        $this->server_info=$this->db->get_array('default');


        //var_dump($this->server_info);
        // db数据库
        $this->m = new MongoClient("mongodb://{$this->server_info['username']}:{$this->server_info['userpwd']}@{$this->server_info['dbip']}");

//        $collection=$this->m->{$this->server_info['dbname']}->PlayerHero;
//        $document=$collection->find();
//        $data=array();
//        //$total = count($document);
//
//        foreach ($document as $key=>$value){
//            //$value['logofftime'] = get_datetime($value['logofftime']);
//            //$value['loginday'] = get_datetime($value['loginday']);
//            $data[$key]=array('id' => $value['_id'], 'cell' => $value);
//        }
//        var_dump($data);
		//$this->dbConnt = getTmpDbConnect(PLATFORM_ALL, 'all', $areanum, DBTYPE_GAMESERVER);
		
//		if (false == $this->dbConnt) {
//			//内部有提示错误，在此就不提示了
//			 //echo "<script>alert(' connect areanum:$areanum error ');</script>";
//            return;
//		}
//		$this->dbConnt->query("SELECT id, name FROM player");
//		while ($n = $this->dbConnt->get_array('default'))
//		{
//			$this->nameList[$n['id']] = $n['name'];
//		}
    }

    function index() {
        $this->view->display("dbplayer/petnew/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $query=$_POST['query'];
        $query_type=$_POST['qtype'];

        if($query_type=='_id'){
            $filter=array($query_type=>(float)$query);
        }else{
            $filter=array($query_type=>$query);
        }
        $collection=$this->m->{$this->server_info['dbname']}->PlayerHero;
        if($query!=''){
            $document=$collection->find($filter);
        }else{
            $document=$collection->find();
        }
        $data=array();
        $total = count($document);

        foreach ($document as $key=>$value){
            //$value['logofftime'] = get_datetime($value['logofftime']);
            //$value['loginday'] = get_datetime($value['loginday']);
            foreach ($value['curheros'] as $k=>$v){
                $v['_id']=$value['_id'];
                $num=$key.$k;
                $data[$num]=array('id' => $value['_id'], 'cell' => $v);
            }

        }
		
//		$sql = "SELECT COUNT(*) AS num FROM player_pet_new  WHERE {$where} {$exwhere}";
//		//echo " sql2:$sql ";
//		$one = $this->dbConnt->one($sql);
//		$total = $one['num'];
//		$orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
//
//		$sql = "SELECT {$colsinfo} FROM player_pet_new WHERE {$where} {$exwhere} {$orderby} LIMIT {$offset},{$rp}";
//		$this->dbConnt->query($sql);
//		//$id = 0;
//		while ($n = $this->dbConnt->get_array('default'))
//		{
//			$n['isfight']=$this->fightlist[$n['isfight']]."-".$n['isfight'];
//			$n['petstatus']=$this->statuslist[$n['petstatus']]."-".$n['petstatus'];
//			$n['roleid']=$this->nameList[$n['roleid']]."(".$n['roleid'].")";
//			$rows[] = array('id' => $n['gmid'], 'cell' => $n);
//			//$id = $id+1;
//		}
		//var_dump($rows);
		$result = array(
	            'page' => $page,
	            'total' => $total,
	            'rows' => $data
		);
		//$this->dbConnt->free_all();
		echo json_encode($result);
    }

}

?>
